
package com.eltonkent.ui.adapters;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.eltonkent.R;
import com.eltonkent.ui.ApplicationConstants;
import com.eltonkent.ui.containers.FolderListContainer;
import com.eltonkent.ui.wrappers.FolderListWrapper;
import com.eltonkent.util.ImageUtil;

public class FolderListAdapter extends ArrayAdapter<FolderListContainer>
{

	int currentTextSize;

	public FolderListAdapter(SharedPreferences prefs, Context context, int textViewResourceId,
			FolderListContainer[] objects)
	{
		super(context, textViewResourceId, objects);
		currentTextSize = prefs.getInt(ApplicationConstants.PREF_TEXT_SIZE,
				ApplicationConstants.PREF_DEF_TEXTSIZE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View listView = convertView;
		FolderListWrapper listWrapper;

		if(listView == null)
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());
			listView = inflater.inflate(R.layout.listview_icon_double_line, parent, false);
			listWrapper = new FolderListWrapper(listView);
			listView.setTag(listWrapper);
		} else
		{
			listWrapper = (FolderListWrapper) listView.getTag();
		}
		FolderListContainer listData = getItem(position);
		String line1Text = listData.getFolderName();
		String line2Text = listData.getFolderDesc();
		listWrapper.getListLine1().setText(line1Text);
		listWrapper.getListLine2().setText(line2Text);

		listWrapper.getListLine1().setTextSize(currentTextSize);
		listWrapper.getListLine2().setTextSize(currentTextSize);
		if(line2Text == null || (line2Text != null && line2Text.equals("")))
		{
			listWrapper.getListLine2().setVisibility(View.GONE);
		}
		String folderIcon = listData.getFolderIcon();
		if(listData.isLocked())
		{

			// Bitmap lock = ImageUtil.createBitmapFromAsset(getContext(),
			// "folder/lock.png");
			// Bitmap lock2 = lock.copy(Bitmap.Config.RGB_565, true);
			// lock.recycle();
			// Bitmap folder = ImageUtil.createBitmapFromAsset(getContext(),
			// "folder/family_locked.png");
			// Bitmap folder2 = folder.copy(Bitmap.Config.RGB_565, true);
			// folder.recycle();
			// folder2 = ImageUtil.overLayBitmap(folder2, lock, 41, 19);
			// listWrapper.getFolderImage().setImageBitmap(folder);
			Log.e("UI", "Folder " + listData.getFolderName() + " is locked!");
			// listWrapper.getLockImage().setVisibility(View.VISIBLE);
			// listWrapper.getLockImage().setImageResource(R.drawable.key);
			StringBuilder builder = new StringBuilder(folderIcon);
			builder.insert(folderIcon.indexOf('.'), "_locked");
			folderIcon = builder.toString();
			Log.d("UI", "Icon after appending->" + builder.toString());

		} else
		{
			// listWrapper.getLockImage().setVisibility(View.GONE);
			// just ADDED

		}
		Bitmap folder = ImageUtil.createBitmapFromAsset(getContext(), folderIcon);
		listWrapper.getFolderImage().setImageBitmap(folder);
		return (listView);
	}
}
